package cn.jack.h264encodeanddecode

import android.annotation.SuppressLint
import android.os.Bundle
import android.os.Environment
import android.util.DisplayMetrics
import android.view.SurfaceHolder
import android.view.SurfaceView
import android.view.View
import androidx.activity.ComponentActivity
import java.io.File

class DecodeActivity : ComponentActivity() {
    @SuppressLint("MissingInflatedId")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContentView(R.layout.activity_main)

        val surface = findViewById<View>(R.id.preview) as SurfaceView

        val displayMetrics = DisplayMetrics()
        windowManager.defaultDisplay.getMetrics(displayMetrics)
        val screenWidth = displayMetrics.widthPixels
        val screenHeight = displayMetrics.heightPixels

        val surfaceHolder = surface.holder
        surfaceHolder.addCallback(object : SurfaceHolder.Callback {
            override fun surfaceCreated(holder: SurfaceHolder) {
                val h264Player = PlayH264Helper(
                    File(
                        Environment.getExternalStorageDirectory(),
                        "encoder_h264.h264"
                    ).absolutePath,
                    surfaceHolder.surface, screenWidth, screenHeight
                )
                h264Player.play()
            }

            override fun surfaceChanged(
                holder: SurfaceHolder,
                format: Int,
                width: Int,
                height: Int
            ) {
            }

            override fun surfaceDestroyed(holder: SurfaceHolder) {}
        })
    }
}